# ！ 生成测试用的虚假数据，并添加到数据库的脚本
from sqlhelper import db
from datetime import datetime
from faker import Faker
import random

fake = Faker('zh_CN')


def fake_admin():
    """一键生成虚拟账户"""
    username = fake.name()
    password = "123456"
    telephone = "13033829000"
    ctime = datetime.now()
    sql = "insert into `user`(username,password,telephone,ctime) values (%s,%s,%s,%s)"
    db.insert_one(sql, [username, password, telephone, ctime])


def fake_categories(count=5):
    """生成虚拟的5种文章分类"""
    for i in range(count):
        name = fake.word()
        sql = "insert into `category`(name) values (%s)"
        try:
            db.insert_one(sql, [name])
        except Exception as e:
            pass


def fake_posts(count=20):
    """生成虚拟的文章"""
    for i in range(count):
        title = fake.sentence()
        body = fake.text(200)
        timestamp = datetime.now()
        # 获取文章分类的种类数
        res = db.fetchall("select * from `category`", [])
        c_num = len(res)
        # 随机获取作者id
        res2 = db.fetchall("select * from `user`", [])
        author_num = len(res2)
        # 随机生成文章分类的id
        category_id = random.randint(1, c_num)
        author_id = random.randint(1, author_num)
        sql = "insert into `post`(title,body,timestamp,category_id,author_id) values (%s,%s,%s,%s,%s)"
        db.insert_one(sql, [title, body, timestamp, category_id, author_id])


def fake_comments(count=10):
    """生成虚拟的评论"""
    # ，非回复评论，非作者自己的评论
    for i in range(count):
        author = fake.name()
        body = fake.sentence()
        timestamp = fake.date_time_this_year()
        reviewed = 1  # 默认已审核
        # 获取文章数量
        res = db.fetchall("select id from `post`", [])
        # 随机生成post文章id
        post_id = random.sample(res, 1)
        post_id = post_id[0].get("id")
        sql = "insert into `comment`(author,body,timestamp,reviewd,post_id) values (%s,%s,%s,%s,%s)"
        db.insert_one(sql, [author, body, timestamp, reviewed, post_id])
    salt = int(count * 0.5)
    ############# 未审核评论和来自作者的评论
    for i in range(salt):
        author = fake.name()
        body = fake.sentence()
        timestamp = fake.date_time_this_year()
        reviewed = 0  # 未审核
        # 随机获得一篇文章id
        res = db.fetchall("select * from `post`", [])
        post_id = random.sample(res, 1)
        post_id = post_id[0].get("id")

        sql = "insert into `comment`(author,body,timestamp,reviewd,post_id) values (%s,%s,%s,%s,%s)"
        db.insert_one(sql, [author, body, timestamp, reviewed, post_id])
        ############# 作者评论
        body = fake.sentence()
        timestamp = fake.date_time_this_year()
        reviewed = 1  # 默认已审核
        # 生成该文章作者名字
        author = db.fetchone("select username from `post` left join `user` on post.author_id=user.id where post.id=%s",
                             [post_id])["username"]
        from_admin = 1
        sql = "insert into `comment`(author,body,timestamp,reviewd,post_id,from_admin) values (%s,%s,%s,%s,%s,%s)"
        db.insert_one(sql, [author, body, timestamp, reviewed, post_id, from_admin])

        # 回复
    ############# 评论回复
    for i in range(salt):
        author = fake.name()
        body = fake.sentence()
        timestamp = fake.date_time_this_year()
        reviewed = 1  # 默认已审核
        # 随机生成post文章id
        res = db.fetchall("select * from `post`", [])
        post_id = random.sample(res, 1)
        post_id = post_id[0].get("id")
        from_admin = 1
        replied_id = random.sample(db.fetchall("select * from `comment`", []), 1)
        # print(replied_id)
        replied_id = replied_id[0].get("id")
        sql = "insert into `comment`(author,body,from_admin,timestamp,reviewd,post_id,replied_id) values (%s,%s,%s,%s,%s,%s,%s)"
        db.insert_one(sql, [author, body, from_admin, timestamp, reviewed, post_id, replied_id])


# 整合上述内容生成的虚拟数据
def faker_forge(categories_count, posts_count, comments_count):
    fake_admin()
    fake_categories(categories_count)
    fake_posts(posts_count)
    fake_comments(comments_count)


# 生成虚拟的账户、文章分类、文章、评论
faker_forge(2, 10, 30)
# fake_comments(10)